<?php

namespace App\Http\Controllers\Admin;

use App\Library\R;
use App\Models\Icon;
use App\Models\Member;
use App\Models\Order;
use App\Models\Permission;
use App\Models\Role;
use App\Models\Shipping;
use App\Models\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Cache;

class IndexController extends Controller
{
    public function test()
    {
        echo json_encode(['qwe','345']);
    }

    /**
     * 后台布局
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function layout()
    {
        //菜单&消息使用AppServiceProvider全局配置
        return view('admin.layout');
    }

    /**
     * 后台首页
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index()
    {
        // 系统信息
        $sys_info = cache('sys_cache_server_info');
        if (!$sys_info) {
            $sys_info = $this->getServerInfo();
            cache(['sys_cache_server_info' => $sys_info], 10 * 60);
        }

        // 统计数据
        $driver = Member::where('role_id', Member::ROLE_DRIVER)->count();
        $owner = Member::where('role_id', Member::ROLE_OWNER)->count();
        $order = Order::count();
        $shipping = Shipping::count();

        return view('admin.index.index', compact('sys_info', 'driver', 'owner', 'order', 'shipping'));
    }

    public function index1()
    {
        return view('admin.index.index1');
    }

    public function index2()
    {
        return view('admin.index.index2');
    }

    public function index3()
    {
        return view('admin.index.index3');
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 数据表格接口
     */
    public function data(Request $request)
    {
        $model = $request->get('model');
        $limit = $request->get('limit', 30);
        switch (strtolower($model)) {
            case 'user':
                $query = new User();
                break;
            case 'role':
                $query = new Role();
                break;
            case 'permission':
                $query = new Permission();
//                $query = $query->where('parent_id', $request->get('parent_id', 0))->with('icon');
                $query = $query->with('icon');
                $limit = 3000;
                break;
            default:
                $query = new User();
                break;
        }
        $res = $query->paginate($limit)->toArray();

        return $this->table($res['data'], $res['total']);
    }

    /**
     * @return \Illuminate\Http\JsonResponse
     * 所有icon图标
     */
    public function icons()
    {
        $icons = Icon::orderBy('sort', 'desc')->get();

        return $this->success('success', $icons);
    }

    /**
     * 获取系统信息
     * @return mixed
     */
    protected function getServerInfo()
    {
        $sys_info['os']           = PHP_OS;
        $sys_info['zlib']         = function_exists('gzclose') ? 'YES' : 'NO'; //zlib
        $sys_info['safe_mode']    = (boolean)ini_get('safe_mode') ? 'YES' : 'NO'; //safe_mode = Off
        $sys_info['timezone']     = function_exists("date_default_timezone_get") ? date_default_timezone_get() : "no_timezone";
        $sys_info['curl']         = function_exists('curl_init') ? 'YES' : 'NO';
        $sys_info['web_server']   = $_SERVER['SERVER_SOFTWARE'];
        $sys_info['phpv']         = phpversion();
        $sys_info['ip']           = GetHostByName($_SERVER['SERVER_NAME']);
        $sys_info['fileupload']   = @ini_get('file_uploads') ? ini_get('upload_max_filesize') : 'unknown';
        $sys_info['max_ex_time']  = @ini_get("max_execution_time") . 's'; //脚本最大执行时间
        $sys_info['domain']       = $_SERVER['HTTP_HOST'];
        $sys_info['memory_limit'] = ini_get('memory_limit');
        $dbPort                   = config('database.prefix');
        $dbHost                   = config('database.prefix');
        $dbHost                   = empty($dbPort) || $dbPort == 3306 ? $dbHost : $dbHost . ':' . $dbPort;

        $musql_version             = \DB::select('select version() as ver');
        $sys_info['mysql_version'] = $musql_version[0]->ver;
        if (function_exists("gd_info")) {
            $gd                 = gd_info();
            $sys_info['gdinfo'] = $gd['GD Version'];
        } else {
            $sys_info['gdinfo'] = "未知";
        }

        return $sys_info;
    }

    /**
     * 清空缓存
     * @return mixed
     */
    public function flush()
    {
        Cache::flush();
        return $this->success('缓存已清除');
    }

}
